#!/usr/bin/python
# -*- coding: utf-8 -*-
#Create Time : 2006 2006-6-18 0:11:06
#Create By User :

# This Script .will lookup all media directory
# Add all media to MediaTable , check MediaID

#===============================================================================
# TODO:
#        check a directory has 'forbidden.html' 
#        TAG: maybe move to show app
#        check a directory has 'intro.html' 
#        TAG: maybe move to show app
#        check a directory has a media uuid ,
#                if not then put it , and add media to MediaTable
#                if has, then check this media path is vaild
#                        if media path is vaild , then skip other action
#                        if media path not vaild , then change media's path
#===============================================================================

from utils import cylons2_utils
import cylons_config
import os
import os.path

# use show models in my script
import sys
sys.path.append( cylons_config.CONF_SITE_PATH )
os.environ["DJANGO_SETTINGS_MODULE"] = "Cylons2.settings"
from show import models

def make_all_dir_lists():
    '''make all directory lists
    '''

    _temp = []
    for root, dirs, files in os.walk( cylons_config.CONF_MEDIA_ROOT_PATH ):
        for dir_name in dirs:
            #print os.path.join( root, dir_name )
            _temp.append( os.path.join( root, dir_name ) )
    return _temp

def check_media():
    cylons2_utils.show_debug( 'Media Scanner start......' )
    for x in make_all_dir_lists():
        #print x
        cylons2_utils.hasMediaID( x )
    cylons2_utils.show_debug( 'Media Scanner stop......' )
    #cylons2_utils.deleteMediaByUUID('21fe7048-b23c-6bf4-1783-81d79bf56c4a')

def reset_media():
    cylons2_utils.show_debug( 'Reset all Media start......' )
    for p in make_all_dir_lists():
        _c = cylons2_utils.getMediaIDFile( p )
        if _c == None:
            pass
        else:
            for i in cylons2_utils.getMediaIDFile( p ):
                cylons2_utils.deleteMediaUUIDFile(p, i)
    #when clean all uuid file , then drop mediatbale
    models.MediaTable.objects.all().delete() #@PydevCodeAnalysisIgnore
    cylons2_utils.show_debug( 'Reset all Media stop......' )

def main():
    '''main program
    '''
    reset_media()
    #check_media()


if __name__ == '__main__':
    main()

